//
//  TXInterstitial.m
//  TestTX
//
//  Created by Akira on 2020/1/19.
//  Copyright © 2020 Akira. All rights reserved.
//


#import "TXInterstitial.h"
#import <Unity/UnityInterface.h>
#import "UnityAppController.h"

@implementation CTXInterstitial
static CTXInterstitial* sharedInstance=nil;
+(CTXInterstitial*)Instance{
    @synchronized (self.class) {
        if(sharedInstance==nil){
            sharedInstance=[[self.class alloc] init];
        }
        
        return sharedInstance;
    }
}

- (IBAction)loadAd{
    printf("TXInterstitial::loadAd()");
    if (self.interstitial) {
        self.interstitial.delegate = nil;
    }
    self.interstitial = [[GDTUnifiedInterstitialAd alloc] initWithAppId:[self strAppID] placementId:[self strUtilID]];
    
    self.interstitial.delegate = self;
    
    [self.interstitial loadAd];
}

/**
 展示广告
 需要传入展示广告的controll
 */
- (IBAction)showAd{
    printf("TXInterstitial::showAd()");
    [self.interstitial presentAdFromRootViewController:[self adViewControll]];
}

/**
 *  插屏2.0广告预加载成功回调
 *  当接收服务器返回的广告数据成功且预加载后调用该函数
 */
- (void)unifiedInterstitialSuccessToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialSuccessToLoadAd()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialSuccessToLoadAd", "");
}

/**
 *  插屏2.0广告预加载失败回调
 *  当接收服务器返回的广告数据失败后调用该函数
 */
- (void)unifiedInterstitialFailToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
    printf("TXInterstitial::unifiedInterstitialFailToLoadAd(): code:%ld, msg:%s", (long)[error code], [[error description] UTF8String]);
    
    char szThem[1024]={};
    sprintf(szThem,"code:%ld, msg:%s", [error code], [[error description]UTF8String]);
    
    UnitySendMessage("GDTAgent", "unifiedInterstitialFailToLoadAd", szThem);
}

/**
 *  插屏2.0广告将要展示回调
 *  插屏2.0广告即将展示回调该函数
 */
- (void)unifiedInterstitialWillPresentScreen:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialWillPresentScreen()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialWillPresentScreen", "");
}

/**
 *  插屏2.0广告视图展示成功回调
 *  插屏2.0广告展示成功回调该函数
 */
- (void)unifiedInterstitialDidPresentScreen:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialDidPresentScreen()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialDidPresentScreen", "");
}

/**
 *  插屏2.0广告视图展示失败回调
 *  插屏2.0广告展示失败回调该函数
 */
- (void)unifiedInterstitialFailToPresent:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
    printf("TXInterstitial::unifiedInterstitialFailToPresent()");
    
    char szThem[1024]={};
    sprintf(szThem,"code:%ld, msg:%s", [error code], [[error description]UTF8String]);
    UnitySendMessage("GDTAgent", "unifiedInterstitialFailToPresent", szThem);
}

/**
 *  插屏2.0广告展示结束回调
 *  插屏2.0广告展示结束回调该函数
 */
- (void)unifiedInterstitialDidDismissScreen:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialDidDismissScreen()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialDidDismissScreen", "");
}

/**
 *  当点击下载应用时会调用系统程序打开其它App或者Appstore时回调
 */
- (void)unifiedInterstitialWillLeaveApplication:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialWillLeaveApplication()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialWillLeaveApplication", "");
}

/**
 *  插屏2.0广告曝光回调
 */
- (void)unifiedInterstitialWillExposure:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialWillExposure()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialWillExposure", "");
}

/**
 *  插屏2.0广告点击回调
 */
- (void)unifiedInterstitialClicked:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialClicked()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialClicked", "");
}

/**
 *  点击插屏2.0广告以后即将弹出全屏广告页
 */
- (void)unifiedInterstitialAdWillPresentFullScreenModal:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdWillPresentFullScreenModal()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdWillPresentFullScreenModal", "");
}

/**
 *  点击插屏2.0广告以后弹出全屏广告页
 */
- (void)unifiedInterstitialAdDidPresentFullScreenModal:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdDidPresentFullScreenModal()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdDidPresentFullScreenModal", "");
}

/**
 *  全屏广告页将要关闭
 */
- (void)unifiedInterstitialAdWillDismissFullScreenModal:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdWillDismissFullScreenModal()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdWillDismissFullScreenModal", "");
}

/**
 *  全屏广告页被关闭
 */
- (void)unifiedInterstitialAdDidDismissFullScreenModal:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdDidDismissFullScreenModal()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdDidDismissFullScreenModal", "");
    [self loadAd];
}

/**
 * 插屏2.0视频广告 player 播放状态更新回调
 */
- (void)unifiedInterstitialAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial playerStatusChanged:(GDTMediaPlayerStatus)status{
    printf("TXInterstitial::unifiedInterstitialAd()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAd", "");
}

/**
 * 插屏2.0视频广告详情页 WillPresent 回调
 */
- (void)unifiedInterstitialAdViewWillPresentVideoVC:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdViewWillPresentVideoVC()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdViewWillPresentVideoVC", "");
}

/**
 * 插屏2.0视频广告详情页 DidPresent 回调
 */
- (void)unifiedInterstitialAdViewDidPresentVideoVC:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdViewDidPresentVideoVC()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdViewDidPresentVideoVC", "");
}

/**
 * 插屏2.0视频广告详情页 WillDismiss 回调
 */
- (void)unifiedInterstitialAdViewWillDismissVideoVC:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdViewWillDismissVideoVC()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdViewWillDismissVideoVC", "");
}

/**
 * 插屏2.0视频广告详情页 DidDismiss 回调
 */
- (void)unifiedInterstitialAdViewDidDismissVideoVC:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    printf("TXInterstitial::unifiedInterstitialAdViewDidDismissVideoVC()");
    UnitySendMessage("GDTAgent", "unifiedInterstitialAdViewDidDismissVideoVC", "");
}
@end

void _LoadTXInterstitialAD(){
    printf("TXInterstitial::_LoadTXInterstitialAD()");
    [[CTXInterstitial Instance]loadAd];
}

void _ShowTXInterstitialAD(){
    printf("TXInterstitial::_ShowTXInterstitialAD()");
    [[CTXInterstitial Instance]showAd];
}

void _InitTXInterstitial(const char* pszAppID, const char* pszUtilID){
    printf("TXInterstitial::_InitTXInterstitial()");
    [CTXInterstitial Instance].strAppID=[NSString stringWithUTF8String:pszAppID];
    [CTXInterstitial Instance].strUtilID=[NSString stringWithUTF8String:pszUtilID];
    
    [CTXInterstitial Instance].adViewControll=_UnityAppController.rootViewController;
}
